package com.example.server.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.web.domain.Academy;
import com.example.server.web.domain.Major;
import com.example.server.web.domain.dto.MajorParamDto;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.domain.vo.MajorVo;
import com.example.server.web.exception.CustomException;
import com.example.server.web.service.AcademyService;
import com.example.server.web.service.MajorService;
import com.example.server.web.mapper.MajorMapper;
import com.example.server.web.utils.DateUtils;
import com.example.server.web.utils.LoginUserUtil;
import com.example.server.web.utils.MyObjectUtils;
import com.example.server.web.utils.UUIDKeyGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author YHJ
* @description  服务实现类
*/
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major>
    implements MajorService{

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private AcademyService academyService;

    @Override
    public Integer add(Major major) {
        major.setCreateById(LoginUserUtil.getLoginUser().getId());
        major.setCreateByTime(DateUtils.getNowDate());
        //添加前校验
        addCheck(major);
        if (StringUtils.isBlank(major.getCode())){
            major.setCode(UUIDKeyGenerator.generateCode("MJ",true));
        }
        return majorMapper.insert(major);
    }

    @Override
    public Map queryByPage(MajorParamDto majorParamDto, PageQueryDto pageQueryDto) {
        HashMap<String, Object> map = new HashMap<>();
        //模糊查询
        LambdaQueryWrapper<Major> wrapper = new LambdaQueryWrapper<>();
        //取出参数
        String name = majorParamDto.getName();
        Long academyId = majorParamDto.getAcademyId();
        wrapper.like(StringUtils.isNotBlank(name),Major::getName,name);
        wrapper.eq(academyId != null,Major::getAcademyId,academyId);
        //查询分页信息
        Page<Major> page = this.page(new Page<>(pageQueryDto.getPage(), pageQueryDto.getLimit()), wrapper);

        //查询信息
        List<Major> list = new ArrayList<>();
        for (Major record : page.getRecords()) {
            MajorVo majorVo = new MajorVo();
            BeanUtils.copyProperties(record,majorVo);
            //查询学院名称
            Academy academy = academyService.getById(record.getAcademyId());
            MyObjectUtils.defaultIfNull(academy,"信息不存在");
            majorVo.setAcademyName(academy.getAcademyName());
            list.add(majorVo);
        }

        //封装map结果
        map.put("total",page.getTotal());
        map.put("list",list);
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Major major) {
        LambdaQueryWrapper<Major> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Major::getName,major.getName());
        Major one = this.getOne(wrapper);
        //判断数据库中是否存在重复的班级名称
        if(one!=null&&one.getId()!=major.getId()){
            throw  new CustomException("已存在该专业名称，请换一个！");
        }
        if (StringUtils.isBlank(major.getCode())){
            major.setCode(UUIDKeyGenerator.generateCode("MJ",true));
        } else {
            //查询是否存在重复学院代码
            LambdaQueryWrapper<Major> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Major::getCode, major.getCode());
            Major two = majorMapper.selectOne(wrapper2);
            if(two!=null&&two.getId()!=major.getId()){
                throw  new CustomException("该专业代码已存在，请重新换一个！");
            }
        }
        boolean b = this.updateById(major);
        return b;
    }

    /**
     * 添加前校验
     * @param major
     */
    private void addCheck(Major major) {
        //查询是否存在重复名字
        LambdaQueryWrapper<Major> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Major::getName, major.getName());
        Major one = majorMapper.selectOne(wrapper1);
        if(one!=null){
            throw  new CustomException("该专业名字已存在，请重新换一个！");
        }
        if (StringUtils.isBlank(major.getCode())) return;
        //查询是否存在重复学院代码
        LambdaQueryWrapper<Major> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Major::getCode, major.getCode());
        Major two = majorMapper.selectOne(wrapper2);
        if(two!=null){
            throw  new CustomException("该专业代码已存在，请重新换一个！");
        }
    }

    @Override
    public Boolean deletes(List<Integer> ids) {
        boolean b = this.removeByIds(ids);
        return b;
    }
}




